package com.csun.cmny.provider.controller.app;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.app.AppDeviceTypeVo;
import com.csun.cmny.provider.model.vo.device.DmcDeviceVo;
import com.csun.cmny.provider.model.vo.yingshi.YingShiMonitorVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chenzheng
 * @description:家属版老人APP设备数据控制器
 * @date 2019/9/26 09:50
 */
@RestController
@RequestMapping(value = "/personData", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcPersonDataController extends BaseController {
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceUserBindService dmcDeviceUserBindService;

    @Resource
    private DmcOldUserBindService dmcOldUserBindService;

    @Resource
    private DmcCsunSleepBindService dmcCsunSleepBindService;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcYingShiChannelService dmcYingShiChannelService;

    @Resource
    private YsBindOldService ysBindOldService;

    /**
     * 设备类型列表查询
     * @param
     * @return
     */
    @RequestMapping(value = "/queryDeviceTypeList", method = RequestMethod.POST)
    public Wrapper queryDeviceTypeList() {
        //从枚举类中查询所有的设备类型
        List<Map<String, String>> maps = DmcDeviceTypeEnum.getMaps();
//        return WrapMapper.ok(maps);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                maps);
    }

    /**
     * 查询用户绑定的设备列表
     * @param
     * @return
     */
    @RequestMapping(value = "/queryBoundDeviceList", method = RequestMethod.POST)
    public Wrapper queryDeviceList() {
        List<AppDeviceTypeVo> vos = dmcDeviceUserBindService.queryDeviceList(getLoginAuthDto().getUserId());
//        return WrapMapper.ok(vos);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vos);
    }

    @PostMapping("/queryDeviceByOldId")
    public Wrapper queryDeviceByOldId(Long id){
        YsBindOld ysBindOld = new YsBindOld();
        ysBindOld.setOldmanId(id);
        //查询萤石摄像头
        List<YsBindOld> deviceUserBinds = ysBindOldService.select(ysBindOld);
        List<AppDeviceTypeVo> appDeviceTypeVoList = new ArrayList<>();
        if(deviceUserBinds.size() > 0){
            List<Long> ids = new ArrayList<>();
            for (YsBindOld d:deviceUserBinds) {
                ids.add(d.getDeviceId());
            }
            List<DmcDeviceVo> vos = dmcDeviceService.queryDeviceListByIdList(ids);
            for (DmcDeviceVo d:vos) {
                AppDeviceTypeVo appDeviceTypeVo = new AppDeviceTypeVo();
                List<DmcDeviceVo> deviceVos = new ArrayList<>();
                appDeviceTypeVo.setDeviceType(d.getDeviceType());
                appDeviceTypeVo.setDeviceTypeName(d.getDeviceName());
                //查询萤石通道号
                List<DmcYingShiChannel> dmcYingShiChannels = dmcYingShiChannelService.selectChannelById(d.getId());
                d.setDmcYingShiChannels(dmcYingShiChannels);
                deviceVos.add(d);
                appDeviceTypeVo.setDmcDeviceVoList(deviceVos);
                appDeviceTypeVoList.add(appDeviceTypeVo);
            }
        }
        DmcCsunSleepBind dmcCsunSleepBind = new DmcCsunSleepBind();
        dmcCsunSleepBind.setOldmanId(id.intValue());
        List<DmcCsunSleepBind> csunSleepBinds = dmcCsunSleepBindService.select(dmcCsunSleepBind);
        AppDeviceTypeVo appDeviceType = new AppDeviceTypeVo();
        List<DmcDeviceVo> vos = new ArrayList<>();
        if(csunSleepBinds.size() >0){
            DmcDevice dmcDevice = dmcDeviceService.selectByKey(csunSleepBinds.get(0).getDeviceId());
            appDeviceType.setDeviceType(dmcDevice.getDeviceType());
            appDeviceType.setDeviceTypeName(dmcDevice.getDeviceName());
            DmcDeviceVo dmcDeviceVo = new DmcDeviceVo();
            BeanUtils.copyProperties(dmcDevice,dmcDeviceVo);
            vos.add(dmcDeviceVo);
            appDeviceType.setDmcDeviceVoList(vos);
        }
        appDeviceTypeVoList.add(appDeviceType);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                appDeviceTypeVoList);
    }

    @PostMapping("/ysCameraBindOld")
    public Wrapper ysCameraBindOld(Long deviceId,Long oldId ){
        //查询该摄像头是否绑定
        YsBindOld ysBindOld = new YsBindOld();
        ysBindOld.setDeviceId(deviceId);
        List<YsBindOld> select = ysBindOldService.select(ysBindOld);
        if (select.size() > 0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed ", null));
        }
        ysBindOld.setOldmanId(oldId);
        Integer i = ysBindOldService.save(ysBindOld);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                i);
    }

}
